/**
    Created by Nic wu
**/
public with sharing class SharePrivilege {
    
    public String SelPUsrName{get;set;}
    public String SelCUsrName{get;set;}
    public String SelPCityName{get;set;}
    public String PUID{get;set;}
    public String CUID{get;set;}
    public String PCID{get;set;}
    public Integer CUsrNum { get; set; }
    public Integer PUsrNum { get; set; }
    public Integer PCityNum{get;set;}
    public List<User> PUsrList = new List<User>();
    public List<User> CUsrList = new List<User>();
    public List<RollingForecast__c> PCityList = new List<RollingForecast__c>();
    public boolean IsShared{get;set;}
    public String errMsg {get;set;}
    public String Msg{get;set;}
    public boolean isRendered{get;set;}
    public List<RollingForecast__c> havForecastCitiesOwner = new List<RollingForecast__c>();
    public String MsgType{get;set;}
    
    //constructor
    public SharePrivilege(){
        //要转移用户list
        PUsrList = [select id, name, ALIAS, USERNAME, Email, DEPARTMENT, TITLE, FUNCTIONDEPARTMENT__C, USERCITY__C, USERREGION__C, 
                            UserSubRegion__c, isCityManager__c from User where isCityManager__c=: true];
        
        //转移到的用户list
        CUsrList = [select id, name, ALIAS, USERNAME, Email, DEPARTMENT, TITLE, FUNCTIONDEPARTMENT__C, USERCITY__C, USERREGION__C, 
                            UserSubRegion__c, isCityManager__c from User where isCityManager__c=: true];
        
        isRendered = true;
    }
    
    public id UsrID{
        get{
            id uid =UserInfo.getUserId();
            return uid;
        }
        set;
    }
    
    public User Usr{
        get{
            List<User> l = [select id, name, email from User where id =: UsrID and isCityManager__c=: true limit 1];
            if(l.size()>0)
                return l[0];
            else
                return null;
        }
        set;
    }
    
    //用户的明细
    public User PUserDetails {
        get{
            User usr = new User();            
            for(Integer i=0;i<PUsrList.size();i++) {
                if(PUsrList[i]!=null){
                    if(PUsrList[i].id == PUID){
                        usr = PUsrList[i];
                        break;
                    }
                }
            }
            return usr;
        }
        set; 
    }
    
    public User CUserDetails{
         get{
             User usr = new User();
             for(Integer i=0;i<CUsrList.size();i++) {
                if(CUsrList[i]!=null){
                    if(CUsrList[i].id == CUID) {
                        usr = CUsrList[i];
                        break;
                    }
                }
            }
            return usr;
         }
         set; 
     }
    
    //要转移的用户options
    public List<SelectOption> PUsrOptions{
        get{
            if(PUsrOptions == null)
                PUsrOptions = new List<SelectOption>();
            else
                PUsrOptions.clear();
            PUsrOptions.add(new SelectOption(String.valueOf(-1), '无'));
            for(Integer i=0;i<PUsrList.size();i++) {
                if(PUsrList[i]!=null) {
                    havForecastCitiesOwner = [select forecastCity__c, id, name from RollingForecast__c where ownerId=: PUsrList[i].id];
                    if(havForecastCitiesOwner.size()>0)
                        PUsrOptions.add(new SelectOption(String.valueOf(i), PUsrList[i].name));
                }
            }
            return PUsrOptions;
        }
        set;
    }
    
    //转移到的用户options
    public List<SelectOption> CUsrOptions{
        get{
            if(CUsrOptions== null)
                CUsrOptions= new List<SelectOption>();
            else
                CUsrOptions.clear();
            
            for(Integer i=0;i<CUsrList.size();i++) {
                if(CUsrList[i]!=null){
                    CUsrOptions.add(new SelectOption(String.valueOf(i), CUsrList[i].name));
                }
            }
            return CUsrOptions;
        }
        set;
    }
    
    public List<SelectOption> PCityOptions{
       get{
            if(PCityOptions == null)
                PCityOptions = new List<SelectOption>();
            else
                PCityOptions .clear();
            
            for(Integer i=0;i<PCityList.size();i++){
                if(PCityList[i]!=null){
                    PCityOptions.add(new SelectOption(String.valueOf(i), PCityList[i].forecastCity__c));
                }
            }
            return PCityOptions ;
        }
        set;
    }
   
    //share
    public void SharePriv() {
        List<RollingForecast__c> ExistedForecasts = [select id, name, ownerId from RollingForecast__c 
                                                                            where ownerId =: PUID and ForecastCity__c =:SelPCityName];
        if(ExistedForecasts.size()>0 && PUID != CUID){
            for(Integer i=0;i<ExistedForecasts.size();i++){
                ExistedForecasts[i].ownerId = CUID;
            }
            update ExistedForecasts;
            IsShared = true;
        }
        else
            IsShared = false;
    }
    
    public PageReference actionSupport() {  
        isShow = false;
        MsgType ='';
        errMsg ='';
        if(PUsrNum!=null){
            if(PUsrNum == -1){
                isRendered =true;
                isShow = true;
                MsgType ='WARNING';
                errMsg ='空白项, 不能转移, 请再选择';
                return null;
            }
            else{
                SelPUsrName = PUsrList[PUsrNum].name;
                PUID = PUsrList[PUsrNum].id;
                PCityList = [select id, name, ForecastCity__c from RollingForecast__c where ownerId =: PUID];
                isRendered =false;
            }
        }        
        if(CUsrNum!=null){
            SelCUsrName = CUsrList[CUsrNum].name;
            CUID = CUsrList[CUsrNum].id;
        }        
        if(PCityNum!=null){
            if(PUsrNum == -1){
                isRendered =false;
                isShow = true;
                MsgType ='WARNING';
                errMsg ='空白项, 不能转移, 请再选择';
                return null;
            }else{
                SelPCityName = PCityList[PCityNum].ForecastCity__c;
                isRendered =false;
            }
        }
        
        return null;
    }

    public boolean isShow = false;
    public boolean MsgShow{
        get{
            MsgShow = isShow;
            return MsgShow;
        }
        set;
    }
    
    public PageReference Cancel() {
        pageReference pr = new pageReference('/home/home/jsp');    
        pr.setredirect(true);
        return pr;
    }

    public PageReference Share() {
        SharePriv();
        isShow = true;
        if(IsShared == true) {
            errMsg ='Share 成功';
            MsgType = 'CONFIRM';
        }            
        else  {
            errMsg ='Share 失败';
            MsgType = 'ERROR';
        }
        return System.currentPageReference();
    }
    
    public String UrlId{
        get{
                return System.currentPageReference().getParameters().get('id');
        }
        set;
    }
}